#include "dialog.h"

/**
 * @定义对话框本身属性
 */
static DLGTEMPLATE DlgInitProgress = 
{
    WS_BORDER | WS_CAPTION,
    WS_EX_NONE,
    120, 150, 400, 130,
    "VAM-CNC initializing",
    0, 0,
    3, NULL, 
    0
};

/**
 * @定义空间
 */ 
static CTRLDATA CtrlInitProgress[] = {
    {
        CTRL_STATIC,
        WS_VISIBLE | SS_SIMPLE,
        10, 10, 380, 16,
        IDC_PROMPTINFO,
        "normal......",
        0
    },
    {
        CTRL_PROGRESSBAR,
        WS_VISIBLE,
        10, 40, 380, 20,
        IDC_PROGRESS,
        NULL,
        0
    },
    {
        CTRL_BUTTON,
        WS_TABSTOP | WS_VISIBLE | BS_DEFPUSHBUTTON,
        170, 70, 60, 25,
        IDOK,
        "determine",
        0,
    }
};

/**
 * @函数描述：非模态对话框消息处理函数
 */ 
static LRESULT InitANonModalDialogBoxProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    static int number;
    struct _DepInfo *info;

    switch (message)
    {
    case MSG_INITDIALOG:
        /* 将对话框参数lParam保存为窗口的附加数据，以备后用 */
        printf("MSG_INITDIALOG\r\n");
        info = (struct _DepInfo *)lParam;
        SetWindowAdditionalData(hDlg, (DWORD)lParam);
        break;
    case MSG_COMMAND:
        /* 从窗口的附加数据中去除保存的对话框参数 */
        info = (struct _DepInfo *)GetWindowAdditionalData(hDlg);
        switch (wParam) 
        {
        case IDOK:
        case IDCANCEL:
            if (info != NULL) {
                printf("info:%d\r\n", info->data);
            }
            DestroyMainWindowIndirect (hDlg); /* 销毁窗口 */
            break;

        default:
            break;
        }
        break;

    case MSG_CLOSE:
        DestroyMainWindowIndirect (hDlg); /* 销毁窗口 */
        // DestroyAllControls(hDlg); /* 销毁对话框里的控件 */
        break;

    default:
        break;
    }
    return DefaultDialogProc(hDlg, message, wParam, lParam);
}


/**
 * @函数描述：模态对话框消息处理函数
 */ 
static LRESULT InitDialogBoxProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    static int number;
    struct _DepInfo *info;

    switch (message)
    {
    case MSG_INITDIALOG:
        /* 将对话框参数lParam保存为窗口的附加数据，以备后用 */
        info = (struct _DepInfo *)lParam;
        SetWindowAdditionalData(hDlg, (DWORD)lParam);
        break;
    case MSG_COMMAND:
        /* 从窗口的附加数据中去除保存的对话框参数 */
        info = (struct _DepInfo *)GetWindowAdditionalData(hDlg);
        switch (wParam) 
        {
        case IDOK:
        case IDCANCEL:
            printf("info:%d\r\n", info->data);
            EndDialog(hDlg, wParam); /* 销毁对话框 */
            break;

        default:
            break;
        }
        break;
    case MSG_CLOSE:
        EndDialog(hDlg, wParam); 
        // DestroyAllControls(hDlg); /* 销毁对话框里的控件 */
        break;
    default:
        break;
    }
    return DefaultDialogProc(hDlg, message, wParam, lParam);
}

/**
 * @函数功能：创建模态对话框（父窗口不能操作）
 */ 
void CreateDialog(HWND hWnd)
{
    struct _DepInfo data;
    data.data = 12345;
    DlgInitProgress.controls = CtrlInitProgress;
    DialogBoxIndirectParam(&DlgInitProgress, hWnd, InitDialogBoxProc, (LPARAM)&data);
}

/**
 * @函数功能：创建非模态对话框
 */ 
void CreateANonModalDialog(HWND hWnd)
{
    struct _DepInfo data;
    data.data = 24568;
    DlgInitProgress.controls = CtrlInitProgress;
    CreateMainWindowIndirectParam(&DlgInitProgress, hWnd, InitANonModalDialogBoxProc, (LPARAM)&data);
}
